#include "../../include/sql.h"
#include "../../include/head.h"

int sql_init()
{
    conn = mysql_init(NULL); // mysql的初始化
    if (!conn)               // 判断是否初始化成功
    {
        printf("mysql init err!\n");
        return EXIT_FAILURE;
    }
    conn = mysql_real_connect(conn, "127.0.0.1",
                              "admin", "admin123", "QQDB", 0, NULL, 0);
    if (conn)
    {
        printf("sql connat success!\n");
    }
    else
        printf("sql connat err!\n");
    // mysql_close(conn);  //关闭mysql连接
    return 0;
}

// 单聊查询 wechat表GroupId = 0 AND (userId = userId OR userId = userId2);
int sql_select_wechar_message(int userId, int userId2, char *data, int *num, int url)
{
    MYSQL_RES *result;
    MYSQL_ROW row; // 代表的是结果集中的一行
    char query[200];
    (*num) = 0;
    memset(query, 0, sizeof(query));
    snprintf(query, sizeof(query),
             "SELECT * FROM wechat WHERE GroupId = 0 AND ((userId = %d AND friendId = %d) OR (userId = %d AND friendId = %d))",
             userId, userId2, userId2, userId);
    int r = mysql_query(conn, query);
    if (r)
    {
        printf("select err!\n");
        mysql_close(conn); // 关闭mysql连接
        return -1;
    }
    else
    {
        result = mysql_store_result(conn);
        if (result)
        {
            while ((row = mysql_fetch_row(result)) != NULL)
            {
                (*num)++;
                char StringValue[256];
                snprintf(StringValue, sizeof(StringValue), "%d^%d^%s^%s^%s^%s^%s^%s^%s^%s^%s^%s^%s",
                         url, 100, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]);
                if (strlen((char *)data) == 0)
                {
                    // 将 StringValue 追加到 data 中
                    snprintf((char *)data, sizeof(StringValue) + 5, "%s", StringValue);
                }
                else
                {
                    snprintf((char *)data + strlen((char *)data), sizeof(StringValue) + 5, "&&&&%s", StringValue);
                }
            }
            printf("\n");
            printf("%shuihua select success !\n", data);
        }
    }
    mysql_free_result(result);
}

// 插入会话消息数据
int sql_insert_char(char *data)
{
    const char delim = '^';
    int arrayLength = 0;
    char **tokens = splitStringCorrect(data, delim, &arrayLength);
    char insertQuery[512];
    snprintf(insertQuery, sizeof(insertQuery),
             "INSERT INTO wechat ( userId, friendId, GroupId, sex, messageType, userName, nickname, message, format) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
             tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6], tokens[7], tokens[8], tokens[9]);
    int r = mysql_query(conn, insertQuery);
    if (r)
    {
        printf("insert err!\n");
        return -1;
    }
    else
    {
        printf("insert success!\n");
    }

    for (int i = 0; i < arrayLength; i++)
    {
        free(tokens[i]);
    }
    free(tokens);
}

// 查询会话窗口列表
int sql_select_ConversationWindow(int userId, char *data, int *num, int url)
{

    // 查询
    MYSQL_RES *result; // 保存结果集
    MYSQL_ROW row;     // 代表的是结果集中的一行

    char query[512]; // 定义一个足够大的缓冲区来存储 SQL 查询语句
    snprintf(query, sizeof(query),
             "SELECT * FROM ConversationWindow WHERE userId != %d "
             //  "UNION ALL "
             //  "SELECT cw.* "
             //  "FROM ConversationWindow cw "
             //  "JOIN GroupMember gm ON gm.userId= %d AND cw.Id = gm.groupId "
             ,
             userId);
    int r = mysql_query(conn, query);
    if (r != 0)
    {
        fprintf(stderr, "SQL find err: %s\n", mysql_error(conn));
    }
    else
    {
        printf("find ok!\n");
        result = mysql_store_result(conn); // 保存查询结果
        // 获取result元素个数
        if (result)
        {
            while ((row = mysql_fetch_row(result)) != NULL)
            {
                (*num)++;
                char StringValue[256];
                snprintf(StringValue, sizeof(StringValue), "%d^%d^%s^%s^%s^%s^%s^%s^%s^%s^%s",
                         url, 100, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]);

                if (strlen((char *)data) == 0)
                {
                    snprintf((char *)data, sizeof(StringValue) + 5, "%s", StringValue);
                }
                else
                {

                    snprintf((char *)data + strlen((char *)data), sizeof(StringValue) + 5, "&&&&%s", StringValue);
                }
            }
        }
        else
        {
            printf("save err!\n");
            mysql_close(conn); // 关闭mysql连接
            return -1;
        }
    }
    mysql_free_result(result);
}
// 公共频道群聊
int sql_select_wechar_public_message(int GroupId, char *data, int *num, int url)
{
    MYSQL_RES *result;
    MYSQL_ROW row; // 代表的是结果集中的一行
    char query[200];
    (*num) = 0;
    memset(query, 0, sizeof(query));
    snprintf(query, sizeof(query),
             "SELECT * FROM wechat WHERE GroupId = %d",
             GroupId);
    printf("\n");
    int r = mysql_query(conn, query);
    if (r)
    {
        printf("select err!\n");
        mysql_close(conn); // 关闭mysql连接
        return -1;
    }
    else
    {
        result = mysql_store_result(conn);
        if (result)
        {
            while ((row = mysql_fetch_row(result)) != NULL)
            {
                (*num)++;
                char StringValue[256];
                snprintf(StringValue, sizeof(StringValue), "%d^%d^%s^%s^%s^%s^%s^%s^%s^%s^%s^%s^%s",
                         url, 100, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]);
                if (strlen((char *)data) == 0)
                {
                    // 将 StringValue 追加到 data 中
                    snprintf((char *)data, sizeof(StringValue) + 5, "%s", StringValue);
                }
                else
                {

                    snprintf((char *)data + strlen((char *)data), sizeof(StringValue) + 5, "&&&&%s", StringValue);
                }
            }
            printf("\n");
        }
        mysql_free_result(result);
    }
}
